package com.loan.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.entity.SysUser;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.utils.DateUtils;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.ExamineDataEntity;
import com.loan.system.mapper.ApplyMapper;
import com.loan.system.mapper.ExamineDataMapper;
import com.loan.system.service.ExamineDataService;
import com.loan.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shiguang on 2022/10/8 16:57
 */
@Service
@RequiredArgsConstructor
public class ExamineDataServiceImpl implements ExamineDataService {

    private final ISysUserService userService;

    private final ExamineDataMapper examineDataMapper;

    private final ApplyMapper applyMapper;

    @Override
    public void addOrUpdateExamineData() {
        String buildDate = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD);
        //获取所有审核人员
        List<SysUser> list = userService.selectExamineList();
        for (SysUser user : list) {
            ExamineDataEntity examineDataEntity = examineDataMapper.selectOne(new QueryWrapper<ExamineDataEntity>()
                    .eq("build_date", buildDate)
                    .eq("user_id", user.getUserId()));
            long oneNum = applyMapper.selectCount(new QueryWrapper<ApplyEntity>().eq("one_examine_by", user.getUserId())
                    .between("one_examine_time", DateUtils.toBeginDateByDay(buildDate), DateUtils.toEndDateByDay(buildDate)));
            long twoNum = applyMapper.selectCount(new QueryWrapper<ApplyEntity>().eq("two_examine_by", user.getUserId())
                    .between("one_examine_time", DateUtils.toBeginDateByDay(buildDate), DateUtils.toEndDateByDay(buildDate)));
            if (examineDataEntity == null) {
                examineDataEntity = new ExamineDataEntity();
                examineDataEntity.setUserId(user.getUserId());
                examineDataEntity.setBuildDate(buildDate);
                examineDataEntity.setCreateTime(new Date());
                //获取分配订单数
                examineDataEntity.setOneNum((int) oneNum);
                examineDataEntity.setTwoNum((int) twoNum);
                examineDataMapper.insert(examineDataEntity);
            } else {
                examineDataEntity.setOneNum((int) oneNum);
                examineDataEntity.setTwoNum((int) twoNum);
                examineDataEntity.setUpdateTime(new Date());
                examineDataMapper.updateById(examineDataEntity);
            }
        }
    }

    @Override
    public TableDataInfo<ExamineDataEntity> selectPageExamineDataList(String beginTime, String endTime, Long oneExamineBy, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamineDataEntity> lqw = new LambdaQueryWrapper<ExamineDataEntity>()
                .between(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime), ExamineDataEntity::getBuildDate, beginTime, endTime)
                .eq(ObjectUtil.isNotNull(oneExamineBy), ExamineDataEntity::getUserId, oneExamineBy)
                .orderByDesc(ExamineDataEntity::getBuildDate);
        Page<ExamineDataEntity> page = examineDataMapper.selectPage(pageQuery.build(), lqw);
        for (ExamineDataEntity examineDataEntity : page.getRecords()) {
            if (examineDataEntity.getOneNum() > 0) {
                //初审未审数
                long num1 = applyMapper.selectCount(new QueryWrapper<ApplyEntity>()
                        .eq("status", "0")
                        .eq("del_flag", "0")
                        .between("one_examine_time", DateUtils.toBeginDateByDay(examineDataEntity.getBuildDate()), DateUtils.toEndDateByDay(examineDataEntity.getBuildDate()))
                        .eq("one_examine_by", examineDataEntity.getUserId()));
                examineDataEntity.setNoOneNum((int) num1);
                examineDataEntity.setOneExamineNum(examineDataEntity.getOneNum() - examineDataEntity.getNoOneNum());
                long num2 = applyMapper.selectCount(new QueryWrapper<ApplyEntity>()
                        .notIn("status", "0", "1")
                        .eq("del_flag", "0")
                        .between("one_examine_time", DateUtils.toBeginDateByDay(examineDataEntity.getBuildDate()), DateUtils.toEndDateByDay(examineDataEntity.getBuildDate()))
                        .eq("one_examine_by", examineDataEntity.getUserId()));
                examineDataEntity.setOnePassNum((int) num2);
            }
            //终审未审数
            if (examineDataEntity.getTwoNum() > 0) {
                long num1 = applyMapper.selectCount(new QueryWrapper<ApplyEntity>()
                        .eq("status", "2")
                        .eq("del_flag", "0")
                        .between("two_examine_time", DateUtils.toBeginDateByDay(examineDataEntity.getBuildDate()), DateUtils.toEndDateByDay(examineDataEntity.getBuildDate()))
                        .eq("two_examine_by", examineDataEntity.getUserId()));
                examineDataEntity.setNoTwoNum((int) num1);
                examineDataEntity.setTwoExamineNum(examineDataEntity.getTwoNum() - examineDataEntity.getNoTwoNum());
            }

        }
        return TableDataInfo.build(page);
    }
}
